﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using System.IO;

namespace Sales
{
    public partial class DateReport : Form
    {
        private static DateReport datereport = null;
        private static DataTable reportDt = null;
        public XskdhBLL xskdhBLL = new XskdhBLL();

        #region 页面属性

        public string NewFileName
        {
            get;
            set;
        }
        #endregion
        public DateReport()
        {
            InitializeComponent();
            MiddleModule.eventSend += new Send(MiddleModule_eventSend);
        }
        void MiddleModule_eventSend(object sender, object receiver, object msg)
        {
            frmFileName frmfilename = sender as frmFileName;
            //数据来源登录页面
            if (null != frmfilename && datereport.ToString() == receiver.ToString())
            {
                NewFileName = msg.ToString();
            }
        }
        private void DateReport_Load(object sender, EventArgs e)
        {
            //    this.dateTimePicker1.Value = DateTime.Now.AddDays(-1);

            GridDataBind("");
        }

        public static DateReport CreateInstance()
        {
            if (null == datereport)
            {
                datereport = new DateReport();
            }
            return datereport;
        }

        private DataTable GetNewData(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr["销售单号"] = "合计";

            dr["数量"] = CalcMoney(dt, "数量").ToString("0.");

            dr["金额"] = CalcMoney(dt, "金额").ToString("0.00");

            dt.Rows.Add(dr);
            return dt;

        }
        private DataTable GetNewData(DataTable dt, int type)
        {
            DataRow dr = dt.NewRow();
            dr["销售单号"] = "合计";

            dr["数量合计"] = CalcMoney(dt, "数量合计").ToString("0.");
            //, hykxf  ,wx ,zfb ,djq ,yhk  ,zrje 
            dr["现金消费"] = CalcMoney(dt, "现金消费").ToString("0.");
            dr["会员卡消费"] = CalcMoney(dt, "会员卡合计").ToString("0.");
            dr["微信消费"] = CalcMoney(dt, "微信消费").ToString("0.");
            dr["支付宝消费"] = CalcMoney(dt, "支付宝消费").ToString("0.");
            dr["代金券消费"] = CalcMoney(dt, "代金券消费").ToString("0.");
            dr["银行卡消费"] = CalcMoney(dt, "银行卡消费").ToString("0.");
            dr["折让金额"] = CalcMoney(dt, "折让金额").ToString("0.");






            dr["金额合计"] = CalcMoney(dt, "金额合计").ToString("0.00");

            dt.Rows.Add(dr);
            return dt;

        }

        private decimal CalcMoney(DataTable dt, string name)
        {
            decimal je = 0;

            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dr.BeginEdit();
                    decimal value = 0;
                    if (dr[name].ToString()!="")
                      value = decimal.Parse(dr[name].ToString());
                  

                    if (name.IndexOf("数量") > -1)
                    {
                        dr[name] = value.ToString("0.");
                    }
                    else
                        dr[name] = value.ToString("0.00");
                    dr.EndEdit();
                    je += value;
                }
            }
            catch { }

            return je;


        }


        private void GridDataBind(string sql)
        {
            int type = 0;
            if (sql == "")
            {

                if (customColorRadioButton1.Checked)
                {
                    sql = "select xsdh 销售单号, sj 销售时间,bmmc  店面部门, khmc  客户,slhj 数量合计 ,xj 现金消费, hykxf  会员卡消费,wx 微信消费,zfb 支付宝消费,djq 代金券消费,yhk 银行卡消费 ,zrje 折让金额,ssjehj 金额合计 from dbo.xskdh   order by sj  desc";
                }
                else
                {
                    type = 1;
                    //  sql = "select xsdh 销售单号, tmh 条码号,spbh 商品编号,spmc 商品名称,ysmc 颜色,cm 尺码,gg 规格,dw  单位,sl 数量,hyzk 会员折扣,je 金额, yyybh 营业员编号 from xskdB a inner join xskdh b on a.djlsh=b.djlsh ";
                    sql = "select xsdh 销售单号, tmh 条码号,spbh 商品编号,spmc 商品名称,gg 规格,dw  单位,sl 数量,hyzk 会员折扣,je 金额, yyybh 营业员编号 from xskdB a inner join xskdh b on a.djlsh=b.djlsh ";

                }
            }
            else
            {
                if (customColorRadioButton2.Checked)
                {
                    type = 1;
                }
            }

            DataTable dt = xskdhBLL.ExecuteDt(sql);
            if (type == 1)
            {
                DataTable newData = GetNewData(dt);
                reportDt = newData.Copy();
                this.dataGridView1.DataSource = newData;
            }
            else
            {
                //  reportDt = dt.Copy();
                DataTable newData = GetNewData(dt, 1);
                reportDt = newData.Copy();
                this.dataGridView1.DataSource = newData;
            }

            //this.dataGridView1.DataSource = dt;

        }

        private void gbtnQuery_Click(object sender, EventArgs e)
        {
            string strTime = "";
            string strWhere = "";
            string sql = "";
            int type = 0;
            GetWhereStr(ref strTime, ref strWhere);
            //销售单

            if (customColorRadioButton1.Checked)
            {
                type = 1;
                sql = "select xsdh 销售单号, sj 销售时间,bmmc  店面部门, khmc  客户,slhj 数量合计 ,xj 现金消费, hykxf  会员卡消费,wx 微信消费,zfb 支付宝消费,djq 代金券消费,yhk 银行卡消费 ,zrje 折让金额,ssjehj 金额合计  from dbo.xskdh  where " + strTime + " and djlsh in (select djlsh from xskdb  where " + strWhere + ") ";

            }
            else
            {
                type = 2;
                sql = "select xsdh 销售单号, tmh 条码号,spbh 商品编号,spmc 商品名称,ysmc 颜色,cm 尺码,gg 规格,dw  单位,sl 数量,hyzk 会员折扣,je 金额, yyybh 营业员编号 from xskdB a inner join xskdh b on a.djlsh=b.djlsh where ( " + strWhere + ")  and " + strTime;
            }
            GridDataBind(sql);

        }
        /// <summary>
        /// 获取条件
        /// </summary>
        /// <returns></returns>
        private void GetWhereStr(ref string strTime, ref string strWhere)
        {
            string startTime = this.dateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00";
            string endTime = this.dateTimePicker2.Value.ToString("yyyy-MM-dd") + " 23:59:59";
            string ctxtKey = this.ctxtKey.Text.Trim();
            strTime = "sj>='" + startTime + "'  and sj<='" + endTime + "' ";
            strWhere = "spbh like '%" + ctxtKey + "%'  or spmc like '%" + ctxtKey + "%'  or tmh like '%" + ctxtKey + "%'";
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int index = this.dataGridView1.CurrentRow.Index;

                string sql = "";
                if (customColorRadioButton1.Checked)
                {
                    string xsdh = this.dataGridView1.Rows[index].Cells[0].Value.ToString();
                    sql = "select xsdh 销售单号, tmh 条码号,spbh 商品编号,spmc 商品名称,ysmc 颜色,cm 尺码,gg 规格,dw  单位,sl 数量,hyzk 会员折扣,je 金额, yyybh 营业员编号 from xskdB a inner join xskdh b on a.djlsh=b.djlsh  where xsdh='" + xsdh + "'";
                    this.customColorRadioButton2.Checked = true;
                    GridDataBind(sql);


                }
                else
                {

                    //string xsdh = this.dataGridView1.Rows[index].Cells[0].Value.ToString();
                    //string tmh = this.dataGridView1.Rows[index].Cells[1].Value.ToString();
                    ////  sql = "delete xskdb where tmh='" + tmh + "'";

                    //sql = "delete xskdb  where tmh='" + tmh + "' and  djlsh in (select djlsh from xskdh where xsdh='" + xsdh + "')";
                    //int ss = SQLHelper.ExecuteSql(sql);

                    //if (ss > 0)
                    //{
                    //    sql = "select xsdh 销售单号, tmh 条码号,spbh 商品编号,spmc 商品名称,ysmc 颜色,cm 尺码,gg 规格,dw  单位,sl 数量,hyzk 会员折扣,je 金额, yyybh 营业员编号 from xskdB a inner join xskdh b on a.djlsh=b.djlsh  where xsdh='" + xsdh + "'";

                    //    GridDataBind(sql);
                    //}
                    //else
                    //{
                    //    MessageBox.Show("删除操作失败！");
                    //}

                }

            }
            catch { }
        }

        private void customColorRadioButton2_Click(object sender, EventArgs e)
        {

        }

        private void RadioClick(object sender, EventArgs e)
        {

            GridDataBind("");
        }

        private void gbtnReport_Click(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToString("yyyyMMdd");
            string filename = "";
            string title = "";
            string startTime = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string endTime = this.dateTimePicker2.Value.ToString("yyy-MM-dd");

            if (customColorRadioButton1.Checked)
            {
                filename = time + "销售日报表";

                if (startTime != endTime)
                {
                    title = startTime + "--" + endTime + " 销售日报表";
                }
                else
                    title = startTime + " 销售日报表";

            }
            else
            {
                filename = time + "销售明细单";

                if (startTime != endTime)
                {
                    title = startTime + "--" + endTime + " 销售明细单";
                }
                else
                    title = startTime + " 销售明细单";

            }
            string msg = filename;

            frmFileName fileName = frmFileName.CreateInstance();
            MiddleModule.SendMessage(this, fileName, msg);
            fileName.ShowDialog();

            if (NewFileName != null && NewFileName != "")
            {
                filename = NewFileName;
            }

            if (reportDt != null && reportDt.Rows.Count > 0)
            {
                NPOIHelper.Export(reportDt, title, filename + ".xls");

                FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = folderBrowserDialog1.SelectedPath + "\\" + filename + ".xls";

                    File.Copy(filename + ".xls", path, true);
                    File.Delete(filename);
                }
            }
        }

    }
}
